package com.redpixy.monit.ping;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpCheck {
	private static final Logger log = LoggerFactory
	.getLogger(HttpCheck.class);
	/**
	 * 测试 URL 是否可用
	 * 
	 * @param strURL
	 * @return
	 */
	public static boolean isUrlAvailable(String strURL) {
		boolean flag = false;
		 HttpClient client = new DefaultHttpClient();
	     HttpGet get = new HttpGet(strURL);
		try {
			 HttpParams params = new BasicHttpParams();
		    // HTTP 协议的版本,1.1/1.0/0.9
		    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		    // 字符集
		    HttpProtocolParams.setContentCharset(params, "UTF-8");
		    HttpProtocolParams.setUserAgent(params, "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)");
		    HttpProtocolParams.setUseExpectContinue(params, true);
		    HttpConnectionParams.setConnectionTimeout(params, 8000);
		    HttpConnectionParams.setSoTimeout(params, 8000);
			get.setParams(params);
			HttpResponse response  =client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				flag = true;
			} else {
				flag = false;
			}
		} catch (Exception e) {
            log.error(""+e);
            get.abort();
			return false;
		}finally{
			 client.getConnectionManager().shutdown();
		}
		return flag;
	}
    /**
     * 测试IP是否可用
     * @param ip
     * @param port
     * @return
     */
    
	public static boolean isIpAvailable(String ip, int port) {
		Socket server = new Socket();
		try {
			InetSocketAddress address = new InetSocketAddress(ip,port);
			server.connect(address, 8000);
			return server.isConnected();
		} catch (UnknownHostException e) {
			 log.error(""+e);
			return false;
		} catch (IOException e) {
			 log.error(""+e);
			return false;
		} finally {
			if (server != null) {
				try {
					server.close();
				} catch (IOException e) {
					 log.error(""+e);
				}
			}
		}
	}
	
	

	/**
	 * 检查设定的地址是否运行正常
	 */
	public static boolean checkIP(String ip, String port) {
		try {
			int port_ = Integer.valueOf(port);
			if (!HttpCheck.isIpAvailable(ip, port_)) {
				for (int i = 0; i < 10; i++) {
					Thread.sleep(5000);
					if (HttpCheck.isIpAvailable(ip, port_)) {
						return true;
					}
				}
				return false;
			}
			return true;
		} catch (Exception e) {
			log.error("运行出现异常", e);
			return false;
		}

	}

	public static boolean checkUrl(String url) {
		try {
			if (!HttpCheck.isUrlAvailable(url)) {
				for (int i = 0; i < 10; i++) {
					Thread.sleep(5000);
					if (HttpCheck.isUrlAvailable(url)) {
						return true;
					}
				}
				return false;
			}
			return true;
		} catch (Exception e) {
			log.error("运行出现异常", e);
			return false;
		}

	}

}